package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如何获取请求参数?q=list
        String method = req.getParameter("q"); //list
        if("list".equals(method)) {
            list(req,resp);
        }else if("delete".equals(method)) {
            delete(req,resp);
        }else if("update".equals(method)) {
            update(req,resp);
        }else if("jump".equals(method)) {
            jump(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void jump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id查询到要修改的留言信息
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook gb = service.getQueryById(Integer.parseInt(req.getParameter("id")));
        req.setAttribute("gb",gb);
        req.getRequestDispatcher("/edit.jsp").forward(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的字符编码集
        req.setCharacterEncoding("utf-8");
        //通过getWriter方法获取PrintWriter输出流对象，主要用于在网页上输出结果
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        //调用Service的方法查询所有留言信息
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.queryAll();
        //将list集合保存到服务器端
        session.setAttribute("list",list);
        //转发到list.jsp网页
        //req.getRequestDispatcher("/list.jsp").forward(req,resp);
        resp.sendRedirect("/MavenWebBasic/list.jsp");
        //需要创建几行几列的表格？2行7列
        //        out.println("<table border='1'>");
        //        out.println("<tr>");
        //        out.println("<td>id</td>");
        //        out.println("<td>name</td>");
        //        out.println("<td>phone</td>");
        //        out.println("<td>email</td>");
        //        out.println("<td>title</td>");
        //        out.println("<td>content</td>");
        //        out.println("<td>createdtime</td>");
        //        out.println("</tr>");
        //        for(int i = 0;i<list.size();i++) {
        //            Guestbook gb = (Guestbook) list.get(i);
        //            out.println("<tr>");
        //            out.println("<td>"+gb.getId()+"</td>");
        //            out.println("<td>"+gb.getName()+"</td>");
        //            out.println("<td>"+gb.getPhone()+"</td>");
        //            out.println("<td>"+gb.getEmail()+"</td>");
        //            out.println("<td>"+gb.getTitle()+"</td>");
        //            out.println("<td>"+gb.getContent()+"</td>");
        //            out.println("<td>"+gb.getCreatedtime()+"</td>");
        //            out.println("</tr>");
        //        }
        //        out.println("</table>");
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的字符编码集
        req.setCharacterEncoding("utf-8");
        //通过getWriter方法获取PrintWriter输出流对象，主要用于在网页上输出结果
        PrintWriter out = resp.getWriter();
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook gb = new Guestbook();
        gb.setId(id);
        gb.setName(name);
        gb.setPhone(phone);
        gb.setEmail(email);
        gb.setTitle(title);
        gb.setContent(content);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        gb.setCreatedtime(sdf.format(new Date()));
        boolean isRight = service.update(gb);
        if(isRight) {
            req.getRequestDispatcher("manager?q=list").forward(req,resp);
        }else {
            out.println("update error......");
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的字符编码集
        req.setCharacterEncoding("utf-8");
        //通过getWriter方法获取PrintWriter输出流对象，主要用于在网页上输出结果
        PrintWriter out = resp.getWriter();
        GuestbookService service = new GuestbookServiceImpl();
        boolean isRight = service.deleteBean(Integer.parseInt(req.getParameter("id")));
        if(isRight) {
            req.getRequestDispatcher("manager?q=list").forward(req,resp);
        }else {
            out.println("delete error......");
        }
    }

}
